/**
 * 创建人:  @author huangpeng    
 * 创建时间:  2023年01月07日 15:04
 * 项目名称:  lemon-blog-cloud
 * 文件名称:  BiUserServiceImpl
 * 文件描述:  @Description: 普通用户service实现类
 * 公司名称:  柠檬
 *
 * All rights Reserved, Designed By 柠檬 
 * @Copyright: 2022-2023
 *
 */
package com.lemon.system.service.impl;

import com.lemon.common.core.domain.Result;
import com.lemon.common.core.exception.ServiceException;
import com.lemon.common.core.text.Convert;
import com.lemon.common.core.utils.StringUtils;
import com.lemon.common.security.service.BaseServiceImpl;
import com.lemon.common.security.utils.OptEntityUtils;
import com.lemon.common.security.utils.SecurityUtils;
import com.lemon.system.api.domain.BiUser;
import com.lemon.system.mapper.BiUserMapper;
import com.lemon.system.service.BiUserService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 包名称：com.lemon.system.service.impl
 * 类名称：BiUserServiceImpl
 * 类描述：普通用户service实现类
 * 创建人：@author huangpeng
 * 创建时间：2023年01月07日 15:04
 */
@Service
public class BiUserServiceImpl extends BaseServiceImpl<BiUser> implements BiUserService  {

    @Autowired
    private BiUserMapper userMapper;

    @Override
    public int insert(BiUser model) {
        // 校验用户编码、邮箱、电话号码不能为空
        checkBaseUser(model);
        // 判断用户编码、邮箱、电话号码是否已存在
        checkBaseUserExist(model);
        OptEntityUtils.preInsert(model);
        return userMapper.insertSelective(model);
    }

    @Override
    public int update(BiUser model) {
        BiUser biUser = userMapper.selectByPrimaryKey(model.getId());
        // 校验用户编码、邮箱、电话号码不能为空
        checkBaseUser(model);
        if (!model.getEmail().equals(biUser.getEmail())) {
            BiUser existUserEmail = userMapper.selectByEmail(model.getEmail());
            if (existUserEmail != null) {
                throw new ServiceException("邮箱已存在！");
            }
        }
        if (!model.getPhone().equals(biUser.getPhone())) {
            BiUser existUserPhone = userMapper.selectByPhone(model.getPhone());
            if (existUserPhone != null) {
                throw new ServiceException("手机号码已存在！");
            }
        }
        if (!model.getUserCode().equals(biUser.getUserCode())) {
            throw new ServiceException("用户登录名不支持修改！");
        }
        model.setPassword(null);
        OptEntityUtils.preUpdate(model);
        return userMapper.updateByPrimaryKeySelective(model);
    }


    @Override
    public List<BiUser> selectBiUserList(BiUser biUser)
    {
        return userMapper.selectBiUserList(biUser);
    }

    @Override
    public List<BiUser> queryBaseUserListByKey(Long[] userIds) {
        return userMapper.queryBaseUserListByKey(userIds);
    }

    @Override
    public List<BiUser> queryBaseUserListByKey(String userIds) {
        if (StringUtils.isNotEmpty(userIds)) {
            Long[] ids = Convert.toLongArray(userIds);
            return userMapper.queryBaseUserListByKey(ids);
        } else {
            return userMapper.queryBaseUserListByKey(null);
        }
    }

    @Override
    public BiUser selectByUserCode(String userCode) {
        return userMapper.selectByUserCode(userCode);
    }

    @Override
    public int resetPwd(BiUser user) {
        BiUser model = new BiUser();
        model.setId(user.getId());
        model.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return userMapper.updateByPrimaryKeySelective(model);
    }

    @Override
    public boolean registerUser(BiUser user) {
        // 判断用户编码、邮箱、电话号码是否已存在
        checkBaseUserExist(user);
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        OptEntityUtils.preInsert(user);
        return userMapper.insertSelective(user) > 0;
    }

    @Override
    public List<BiUser> queryUserList(Long[] userIds) {
        return userMapper.selectUserByIds(userIds);
    }

    @Override
    public BiUser selectByEmail(String email) {
        return userMapper.selectByEmail(email);
    }

    @Override
    public BiUser selectByPhone(String phone) {
        return userMapper.selectByPhone(phone);
    }

    @Override
    public BiUser selectByUuidAndSource(String originUid, String thirdSource) {
        return userMapper.selectByUuidAndSource(originUid, thirdSource);
    }

    /**
     * 校验用户是否已经存在
     * @Title: checkBaseUserExist
     * @Description: 校验用户是否已经存在
     * @param model 用户信息
     * @return: void
     * @throws:
     * @author: huangpeng
     * @Date: 2023/11/18 19:26
     */
    private void checkBaseUserExist(BiUser model) {
        BiUser existUser = userMapper.selectByUserCode(model.getUserCode());
        BiUser existUserEmail = userMapper.selectByEmail(model.getEmail());
        BiUser existUserPhone = userMapper.selectByPhone(model.getPhone());
        if (existUser != null) {
            throw new ServiceException("登录名已存在！");
        }
        if (existUserEmail != null) {
            throw new ServiceException("邮箱已存在！");
        }
        if (existUserPhone != null) {
            throw new ServiceException("电话号码已存在！");
        }
    }

    /**
     * 校验用户
     * @Title: checkBaseUser
     * @Description: 校验用户
     * @param model 用户信息
     * @return: void
     * @throws:
     * @author: huangpeng
     * @Date: 2023/11/5 18:33
     */
    private void checkBaseUser(BiUser model) {
        Assert.hasText(model.getEmail(), "邮件地址不能为空!");
        Assert.hasText(model.getPhone(), "电话号码不能为空!");
        Assert.hasText(model.getUserCode(), "登录名不能为空!");
    }
}
